<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
        <el-button type="primary"  class="btn-add"
                   @click="handleAddMenu()"
                   style="padding: 12px 40px;">添加
        </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="menuTable"
                style="width: 100%"
                :data="list"
                border>  <!-- v-loading="listLoading"-->
        <el-table-column label="id" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="名称" align="center">
          <template slot-scope="scope">{{ scope.row.cat_name }}</template>
        </el-table-column>
        <el-table-column label="目录名称" align="center">
          <template slot-scope="scope">{{ scope.row.cat_directory }}</template>
        </el-table-column>
        <el-table-column label="是否开启" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleHiddenChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.cat_status">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否在前台显示" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleHiddenChangeDisplay(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.cat_isshow">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center">
          <!--<template slot-scope="scope">{{ scope.row.cat_listorder }}</template>-->
          <template slot-scope="scope">
            <el-form :model="scope.row">
              <el-form-item size="mini">
                <el-input v-enter-number v-model="scope.row.cat_listorder" @blur="changeNum(scope.$index, scope.row)"></el-input>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">{{ scope.row.cat_createTime }}</template>
        </el-table-column>
        <el-table-column label="设置" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              :disabled="scope.row.id==''"
              @click="handleShowNextLevel(scope.row)">查看下级
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
    <!--    <div>
          {{routers}}
        </div>-->


    <el-dialog
      @close="closeDialog('ruleForm')"
      :title="isEdit?'编辑':'添加'"
      :visible.sync="dialogVisible" top="7vh"
      width="60%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm"
               label-position="left">
        <el-row>
          <el-col>
            <div class="grid-content bg-purple">
              <el-form-item label="分类名称：" prop="cat_name">
                <el-input v-model="ruleForm.cat_name" placeholder="请输入分类名称"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="分类所属级别【一级】：">
              <div class="grid-content bg-purple">
                <el-select v-model="ruleForm.cat_parentid" @change="queryTwoLevel(ruleForm.cat_parentid)" filterable
                           placeholder="请选择分类所属级别【一级】">
                  <el-option v-for="item in oneLevel" :key="item.id" :label="item.cat_name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="分类所属级别【二级】：" >
              <div class="grid-content bg-purple">
                <el-select v-model="ruleForm.cat_secondid" filterable placeholder="请选择分类所属级别【二级】">
                  <el-option v-for="item in twoLevel" :key="item.id" :label="item.cat_name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <div class="grid-content bg-purple">
              <el-form-item label="分类储存目录名称：" prop="cat_directory">
                <el-input v-model="ruleForm.cat_directory" placeholder="请输入分类储存目录名称"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <div class="grid-content bg-purple">
              <el-form-item label="SEO优化标题：" prop="cat_seoTitle">
                <el-input v-model="ruleForm.cat_seoTitle" placeholder="请输入分类SEO优化标题"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <div class="grid-content bg-purple">
              <el-form-item label="SEO优化关键词：" prop="cat_seoKeywords">
                <el-input v-model="ruleForm.cat_seoKeywords" placeholder="请输入分类SEO优化关键词"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <div class="grid-content bg-purple">
              <el-form-item label="SEO优化描述：" prop="cat_seoDescription">
                <el-input type="textarea" v-model="ruleForm.cat_seoDescription" placeholder="请输入分类SEO优化描述"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-form-item label="分类是否开启：" prop="cat_status">
          <el-radio-group v-model="ruleForm.cat_status">
            <el-radio :label="0">不开启</el-radio>
            <el-radio :label="1">开启</el-radio>
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="分类是否在前台显示：" prop="cat_isshow">
          <el-radio-group v-model="ruleForm.cat_isshow">
            <el-radio :label="0">不显示</el-radio>
            <el-radio :label="1">显示</el-radio>
            >
          </el-radio-group>
        </el-form-item>
        <el-row>
          <el-col>
            <div class="grid-content bg-purple">
              <el-form-item label="分类排序id：" prop="cat_listorder">
                <el-input v-enter-number v-model="ruleForm.cat_listorder" placeholder="请输入分类排序id"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  menuList,
  menuDel,
  menuAjax,
  menuPageList,
  cateList,
  cateAjax,
  cateDel,
  courseStyle,
  cateAdd,
  cateInfo,
  cateEdit,
  cateComList
} from '@/api/menu'
import {mapGetters} from 'vuex'
import {groupAdd, groupEdit} from "@/api/role";

export default {
  name: "menuList",
  data() {
    return {
      catId: '',
      isEdit: false,
      list: [],
      total: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      parentId: 0,
      styleContent: '',
      mainBody: '',
      title: '',
      pic: '',
      js: '',
      tId: '',
      activeName: 'first',
      editorText: '', // 双向同步的变量
      editorTextCopy: '',// content-change 事件回掉改变的对象
      dialogImageUrl: '',
      dialogVisible: false,
      ruleForm: {
        cat_name: '',
        cat_parentid: '',
        cat_secondid: '',
        cat_directory: '',
        cat_seoTitle: '',
        cat_seoKeywords: '',
        cat_seoDescription: '',
        cat_status: '',
        cat_isshow: '',
        cat_listorder: '',
      },
      rules: {
        cat_name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'}
        ],
        cat_parentid: [
          {required: true, message: '请选择分类所属级别一级分类', trigger: 'change'}
        ],
        cat_secondid: [
          {required: true, message: '请选择分类所属级别二级分类', trigger: 'change'}
        ],
        cat_directory: [
          {required: true, message: '请输入分类储存目录名称', trigger: 'blur'}
        ],
        cat_seoTitle: [
          {required: true, message: '请输入分类SEO优化标题', trigger: 'blur'}
        ],
        cat_seoKeywords: [
          {required: true, message: '请输入分类SEO优化关键词', trigger: 'blur'}
        ],
        cat_seoDescription: [
          {required: true, message: '请输入分类SEO优化描述', trigger: 'blur'}
        ],
        cat_status: [
          {required: true, message: '请选择分类是否开启', trigger: 'change'}
        ],
        cat_isshow: [
          {required: true, message: '请选择分类是否在前台显示', trigger: 'change'}
        ],
        cat_listorder: [
          {required: true, message: '请输入分类排序id', trigger: 'blur'}
        ]
      },
      drawer: false,
      options: [{
        value: '1',
        label: '黄金糕'
      }, {
        value: '2',
        label: '双皮奶'
      }, {
        value: '3',
        label: '蚵仔煎'
      }, {
        value: '4',
        label: '龙须面'
      }, {
        value: '5',
        label: '北京烤鸭'
      }],
      fileList: [{
        name: 'logo.jpeg',
        url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
      }],
      oneLevel: [],
      twoLevel: []
    }
  },
  created() {
    this.resetParentId();
    this.getList();
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'routers'
    ]),
  },
  watch: {
    $route(route) {
      this.resetParentId();
      this.getList();
    }
  },
  mounted() {
    courseStyle({typeid: 'a'}).then(response => {
      console.log(response);
      this.mainBody = response.data
    });
    this.getOneLevel()
  },
  methods: {
    /**
     * 一级切换数据
     */
    queryTwoLevel(value) {
      console.log(value);
      cateComList({
        cate_parentid: value,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.twoLevel = response.data
      })
    },
    /**
     * 获取一级数据
     */
    getOneLevel() {
      cateComList({
        cate_parentid: 0,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.oneLevel = response.data
      })
    },
    /**
     * 获取每条详情
     */
    getInfo(id) {
      cateInfo(id).then(response => {
        console.log(response);
        this.ruleForm = response.data
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    /**
     * 新增/修改
     * @param formName
     */
    submitForm(formName) {
      this.$refs[ formName ].validate((valid) => {
        if ( valid ) {
          this.$confirm('是否要确认?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if ( this.isEdit ) {
              cateEdit(this.ruleForm).then(response => {
                this.listLoading = false;
                console.log(response);
                this.$message({
                  message: '修改成功！',
                  type: 'success'
                });
                this.dialogVisible = false;
                this.getList();
              });
            } else {
              cateAdd({
                cat_directory: this.ruleForm.cat_directory,
                cat_isshow: this.ruleForm.cat_isshow,
                cat_listorder: this.ruleForm.cat_listorder,
                cat_name: this.ruleForm.cat_name,
                cat_parentid: this.ruleForm.cat_parentid===''?0:this.ruleForm.cat_parentid,
                cat_secondid: this.ruleForm.cat_secondid===''?0:this.ruleForm.cat_secondid,
                cat_seoDescription: this.ruleForm.cat_seoDescription,
                cat_seoKeywords: this.ruleForm.cat_seoKeywords,
                cat_seoTitle: this.ruleForm.cat_seoTitle,
                cat_status: this.ruleForm.cat_status,
              }).then(response => {
                this.listLoading = false;
                console.log(response);
                this.$message({
                  message: '新增成功！',
                  type: 'success'
                });
                this.dialogVisible = false;
                this.getList();
              });
            }
          }).catch(e => e);
        }
      });
    },
    closeDialog(formName) {
      this.$refs[ formName ].resetFields();
    },
    resetForm(formName) {
      this.$refs[ formName ].resetFields();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 5 个课程图`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    resetParentId() {
      this.listQuery.pageNum = 1;
      if ( this.$route.query.parentId != null ) {
        this.parentId = this.$route.query.parentId;
      } else {
        this.parentId = 0;
      }
    },
    /**
     * 跳转添加页面
     */
    handleAddMenu() {
      /*this.$router.push('/classifyAdd');*/
      this.dialogVisible = true;
      this.isEdit = false;
    },
    /**
     * 获取课程列表分类数据
     */
    getList() {
      this.listLoading = true;
      cateList({
        cat_parentid: this.catId,
        page: this.listQuery.pageNum,
        page_size: this.listQuery.pageSize
      }).then(response => {
        this.listLoading = false;
        console.log(response);
        this.list = response.data.data_list
        this.total = response.data.total
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    /**
     * 是否开启
     * @param index
     * @param row
     */
    handleHiddenChange(index, row) {
      cateAjax({id: row.id, field: "cat_status", value: row.cat_status}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 是否在前台显示
     * @param index
     * @param row
     */
    handleHiddenChangeDisplay(index, row) {
      cateAjax({id: row.id, field: "cat_isshow", value: row.cat_isshow}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 排序
     * @param index
     * @param row
     */
    changeNum(index, row) {
      cateAjax({id: row.id, field: "cat_listorder", value: row.cat_listorder}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 查看下级
     */
    handleShowNextLevel(row) {
      console.log(row)
      this.catId = row.id;
      cateList({
        cat_parentid: row.id,
        page: this.listQuery.pageNum,
        page_size: this.listQuery.pageSize
      }).then(response => {
        this.list = response.data.data_list
        this.total = response.data.total
      });
      /*this.$router.push({path: '/ums/menu', query: {parentId: row.id}})*/
    },
    /**
     * 单条修改
     * @param index
     * @param row
     */
    handleUpdate(index, row) {
      this.rowId = row.id;
      this.dialogVisible = true;
      this.isEdit = true;
      this.getInfo(row.id)
      /*      console.log(row);
            this.$router.push({path: '/classifyUpdate', query: {id: row.id}});*/
    },
    handleDelete(index, row) {
      this.$confirm('是否要删除该菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cateDel(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          });
          this.getList();
        });
      }).catch(e => e);
    }
  },
  filters: {
    levelFilter(value) {
      if ( value === 0 ) {
        return '一级';
      } else if ( value === 1 ) {
        return '二级';
      }
    },
    disableNextLevel(value) {
      console.log(value);
      if ( value === 0 ) {
        return false;
      } else {
        return true;
      }
    }
  }
}
</script>

<style scoped>
.btn-add {
  float: right;
}

/deep/ .el-pagination {
  padding: 10px 5px;
}

/deep/ .el-drawer {
  overflow-y: scroll;
}

.table {
  margin-bottom: 20px;
}

.my /deep/ .el-radio__inner {
  width: 18px;
  height: 18px;
}

.my /deep/ .el-radio__inner::after {
  width: 6px;
  height: 6px;
}

.small-button /deep/ .el-upload-list {
  display: none;
}

</style>
<style lang="scss" scoped>
.el-icon-caret-left {
  position: absolute;
  top: 20%;
  left: -65px;
  font-size: 80px;
  color: #1f2d3d;
}

.mbys-box-right {
  position: absolute;
  top: 0px;
  right: 0px;
  height: 100%;
}

.div-button {
  width: 10%;
}

.div-button:nth-child(2) {
  margin-left: 100px;
}

.dashboard-box-auto, .dashboard-box-bottom {
  margin-top: 15px;
}

.dashboard-container ul {
  margin: 0px;
  padding: 0px;
}

.dashboard-container li {
  list-style: none;
}

.dashboard-box-right > ul {
  font-size: 0;
  width: 100%;
}

.dashboard-box-right > ul > li:nth-child(1) {
  display: block;
  width: 100%;
  margin: 0;
  border: 5px solid #cccccc;
}

.dashboard-box-right > ul > li:nth-child(2) {
  margin-left: 0;
}

.dashboard-box-right > ul > li {
  width: 22%;
  font-size: 0;
  margin-left: 4%;
  margin-top: 15px;
  border: 3px solid #cccccc;
  display: inline-block;
}

.dashboard-box-right > ul > li > img {
  width: 100%;
}

.el-checkbox.is-bordered {
  margin: 0px;
}

.el-select {
  width: 100%;
}

.dashboard-box-top {
  display: flex;
}

.dashboard-box-left {
  width: 50%;
}

.dashboard-box-right {
  width: 26%;
  margin-left: 10%;
}

.el-radio {
  &-inner {
    width: 18px;
    height: 18px;
  }
}

.dashboard {
  &-container {
    margin: 0 15px;
    padding: 30px;
    position: relative;
    background-color: #fff;
  }

  &-text {
    font-size: 16px;
  }
}
</style>
